package com.molichuxing.gateway.bff.orders.services.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.molichuxing.framework.exception.BizException;
import com.molichuxing.gateway.bff.orders.convertor.Convert;
import com.molichuxing.gateway.bff.orders.entity.response.RepaymentVo;
import com.molichuxing.gateway.bff.orders.entity.response.ScheduleVo;
import com.molichuxing.gateway.bff.orders.services.OrderScheduleBffService;
import com.molichuxing.services.infrastructure.dto.response.OrderExclusivelDto;
import com.molichuxing.services.infrastructure.dto.response.OrderScheduleDto;
import com.molichuxing.services.infrastructure.service.OrderExclusivelService;
import com.molichuxing.services.infrastructure.service.OrderScheduleService;
import com.molichuxing.services.property.OrderScheduleStatusEnum;

@Service("orderScheduleBffService")
public class OrderScheduleBffServiceImpl implements OrderScheduleBffService {
	private static final Logger logger = LoggerFactory.getLogger(OrderScheduleBffServiceImpl.class);

	@Resource
	private OrderExclusivelService orderExclusivelService;

	@Resource
	private OrderScheduleService orderScheduleService;

	/**
	 * 查询用户订单还款信息
	 * 
	 * @param userId
	 * @param orderCode
	 * @return
	 */
	@Override
	public RepaymentVo getExclusivelRepayment(Integer userId, Long orderCode) throws BizException, Exception {
		RepaymentVo result = null;
		// 根据订单号查询专属里程订单信息
		OrderExclusivelDto orderExclusivelDto = orderExclusivelService.getOrderExclusivel(orderCode);
		if (orderExclusivelDto == null) {
			logger.error("[getExclusivelDetails]根据订单号(" + orderCode + ")查询订单异常，订单不存在");
			throw new BizException("查询失败，订单不存在");
		} else if (!orderExclusivelDto.getUserId().equals(userId)) {
			logger.error("[getExclusivelDetails]根据订单号(" + orderCode + ")、用户(" + userId + ")查询订单异常，订单用户信息不匹配");
			throw new BizException("查询失败，订单不存在");
		}

		// 查询该订单排期信息
		List<OrderScheduleDto> orderScheduleDtos = orderScheduleService
				.getOrderSchedule(orderExclusivelDto.getOrderCode());
		if (orderScheduleDtos == null || orderScheduleDtos.isEmpty()) {
			logger.error("[exclusivelPayInfo]订单(" + orderExclusivelDto.getOrderCode() + ")对应排期信息异常");
			throw new BizException("查询失败，订单异常");
		}

		result = new RepaymentVo();
		Integer totalPeriods = 0;
		List<ScheduleVo> schedules = new ArrayList<>(orderScheduleDtos.size());
		for (OrderScheduleDto orderScheduleDto : orderScheduleDtos) {
			ScheduleVo schedule = Convert.toScheduleVo(orderScheduleDto);
			// 判断是否活动减免金额
			BigDecimal activityAmount = orderExclusivelDto.getRental().subtract(orderScheduleDto.getPayable());
			if (activityAmount.intValue() > 0 && orderScheduleDto.getPeriods() == 2) {
				schedule.setIsActivity(true);
				schedule.setActivityAmount(activityAmount);
			}
			// 当前需要支付信息
			if (OrderScheduleStatusEnum.NO.equals(orderScheduleDto.getStatus())) {
				totalPeriods += 1;
				if (result.getPeriods() == null) {
					result.setScheduleId(orderScheduleDto.getId());
					result.setPeriods(orderScheduleDto.getPeriods());
					result.setPayable(orderScheduleDto.getPayable());
				}
			} else if (OrderScheduleStatusEnum.TERMINATION.equals(orderScheduleDto.getStatus())) {
				result.setIsPayOff(2);
			}

			schedules.add(schedule);
		}
		result.setIsPayOff(result.getIsPayOff() != null ? result.getIsPayOff() : totalPeriods == 0 ? 1 : 0);
		result.setTotalPeriods(totalPeriods);
		result.setSchedules(schedules);
		return result;
	}

}
